package SchemaVo::ModelPropertyVo;

use strict;
use warnings;
use utf8;

use lib './src';

use Constants qw(
    DataType_Unknown
    DataType_Json
    DataType_Text
    DataType_String
    DataType_String
    DataType_String128
    DataType_String256
    DataType_String512
    DataType_DateTime
    DataType_DateTime
    DataType_DateTime
    DataType_Bool
    DataType_Collection
    DataType_DateTime
    DataType_Bool
    AccessType_Unknown
    AccessType_Public
    AccessType_Protected
    AccessType_Private
);

# 从属性参数创建
sub New {
    my ($class, %param) = @_;

    my $self = {
        # 模型属性 Id
        ModelPropertyId => undef,
        # 属性名称
        Name => undef,
        # 数据类型名称
        DataType => undef,
        # 访问类型
        AccessType => undef,
        # 属性说明
        Comment => undef,
        # 默认值
        DefaultValue => undef,
    };

    foreach my $key (keys %param){
        $self->{$key} = $param{$key};
    }

    bless $self, $class;

    return $self;
}

# 从属性实体创建
sub FromProperty {
    my ($class, $property) = @_;

    my $self = {};

    $self = {
        # 模型属性 Id
        ModelPropertyId => $property->Id,
        # 属性名称
        Name => $property->Name,
        # 数据类型名称
        DataType => undef,
        # 访问类型
        AccessType => undef,
        # 属性说明
        Comment => $property->Comment,
        # 默认值
        DefaultValue => $property->DefaultValue,
    };

    bless $self, $class;

    my $dataType = $self->GetDataType($property->DataType);
    my $accessType = $self->GetAccessType($property->AccessType);

    $self->{DataType} = $dataType;
    $self->{AccessType} = $accessType;

    return $self;
}

sub GetDataType{
    my ($self, $dataType) = @_;

    my %typeMap = (
        DataType_Unknown, 'Unknown',
        DataType_Json, 'Json',
        DataType_Text, 'Text',
        DataType_String, 'string',
        DataType_String, 'string64',
        DataType_String128, 'string128',
        DataType_String256, 'string256',
        DataType_String512, 'string512',
        DataType_DateTime, 'DateTime',
        DataType_DateTime, 'DateTimeOffset',
        DataType_DateTime, 'DateOnly',
        DataType_Bool, 'bool',
        DataType_Collection, 'List',
        DataType_DateTime, 'UpdateTime',
        DataType_Bool, 'Deleted',
    );

    return $typeMap{$dataType};
}

# 获取 DataType 枚举
sub GetDataTypeEnum {
    my ($self) = @_;
    my %typeMap = (
        Unknown => DataType_Unknown,
        Json => DataType_Json,
        Text => DataType_Text,
        string => DataType_String,
        string64 => DataType_String,
        string128 => DataType_String128,
        string256 => DataType_String256,
        string512 => DataType_String512,
        DateTime => DataType_DateTime,
        DateTimeOffset => DataType_DateTime,
        DateOnly => DataType_DateTime,
        bool => DataType_Bool,
        List => DataType_Collection,
        UpdateTime => DataType_DateTime,
        Deleted => DataType_Bool,
    );

    if($self->{DataType} =~ /^List<\w+>$/){
        return $typeMap{List};
    }

    return $typeMap{$self->{DataType}};
}

sub GetAccessType {
    my ($self, $accessType) = @_;

    my %typeMap = (
        AccessType_Unknown, '+',
        AccessType_Public, '+',
        AccessType_Protected, '~',
        AccessType_Private, '-',
    );

    return %typeMap{$accessType};
}

# 获取访问类型枚举
sub GetAccessTypeEnum {
    my ($self) = @_;

    my %typeMap = (
        '+' => AccessType_Public,
        '~' => AccessType_Protected,
        '-' => AccessType_Private,
    );

    return %typeMap{$self->{AccessType}};
}

# 返回普通的 Hash 表
sub ToPlainHash{
    my ($self) = @_;

    return {
        # 模型属性 Id
        ModelPropertyId => $self->{ModelPropertyId},
        # 属性名称
        Name => $self->{Name},
        # 数据类型名称
        DataType => $self->{DataType},
        # 访问类型
        AccessType => $self->{AccessType},
        # 属性说明
        Comment => $self->{Comment},
        # 默认值
        DefaultValue => $self->{DefaultValue},
    }
}

1;
